import BasePageCtrl from "./BasePageCtrl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SettingPageCtrl extends BasePageCtrl {
@property(cc.Node)
ui: cc.Node = null;
isShake: boolean = true;

onLoad() {
    this.onUILoad();
    this.addButtonListen();
}

start() {}

onUILoad() {
    this.ui = this.node.addComponent(y);
}

addButtonListen() {
   UiManager.addButtonListen(this.ui.yonghu_item, this.touch_user, this);
   UiManager.addButtonListen(this.ui.yinsi_item, this.touch_privacy, this);
   UiManager.addButtonListen(this.ui.server_item, this.feedback, this);
   UiManager.addButtonListen(this.ui.about_item, this.about, this);
   UiManager.addButtonListen(this.ui.btn_close, this.clickClose, this);
   UiManager.addButtonListen(this.ui.btn_logout, this.logoff, this);
   UiManager.addButtonListen(this.ui.btn_loginout, this.loginOut, this);
   UiManager.addButtonListen(this.ui.music_item, this.musicTouch, this);
   UiManager.addButtonListen(this.ui.sound_item, this.audioTouch, this);
   UiManager.addButtonListen(this.ui.recon_item, this.reconTouch, this);
   UiManager.addButtonListen(this.ui.shake_item, this.shakeTouch, this);
}

_init() {
    c.log("设置界面初始化");
    this.refreshView();
}

refreshView() {
    var e = this;
    this.ui.label_name.getComponent(cc.Label).string = u.subUserName(l.user_name, 6) || "游客";
    this.ui.label_id.getComponent(cc.Label).string = l.user_id;
    this.changeSwitchState(this.ui.spr_music_on, n.getInstance().getMusicState());
    this.changeSwitchState(this.ui.spr_sound_on, n.getInstance().getAudioState());
    this.changeSwitchState(this.ui.spr_shake_on, n.getInstance().getVibratorState());
    this.setRecon();
    l.wx_head && u.loadRemoteImg(l.wx_head).then(function (t) {
        e.ui.spr_head.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(t);
    }).catch( (e) => {
        c.log(e);
    });
    s.reviewing;
}

musicTouch() {
    if (n.getInstance().getMusicState()) {
        n.getInstance().closeBg();
        n.getInstance().pauseMusic("bgm", true);
        n.getInstance().pauseMusic("bgm_outSide", true);
    } else {
        n.getInstance().openBg();
        n.getInstance().pauseMusic("bgm", true);
        n.getInstance().playMusic("bgm_outSide", true, true);
    }
    this.changeSwitchState(this.ui.spr_music_on, n.getInstance().getMusicState());
}

audioTouch() {
    n.getInstance().getAudioState() ? n.getInstance().closeAudio() : n.getInstance().openAudio();
    this.changeSwitchState(this.ui.spr_sound_on, n.getInstance().getAudioState());
}

reconTouch() {
    var e = this;
    c.log("请求个性化推荐接口");
    h.recoSwitch(function () {
        e.setRecon();
    });
}

setRecon() {
    this.changeSwitchState(this.ui.spr_recon_on, 1 == l.reco_switch);
}

shakeTouch() {
    var e = n.getInstance().getVibratorState();
    e ? n.getInstance().closeVibrator() : n.getInstance().openVibrator();
    this.changeSwitchState(this.ui.spr_shake_on, !e);
}

changeSwitchState(e, t) {
    if (e) {
        t ?UiManager.loadSpriteFrame(e, "recon", "set_on") :UiManager.loadSpriteFrame(e, "recon", "set_off");
    }
}

touch_user() {
    c.log("用户协议");
    d.showPage("WebPage", {
        type: g.WebUrlType.USER_TYPE
    });
}

touch_privacy() {
    c.log("隐私条款");
    d.showPage("WebPage", {
        type: g.WebUrlType.PRIVACY_TYPE
    });
}

logoff() {
    c.log("账号注销界面");
    d.showPage("LoginOutPage");
}

loginOut() {
    u.localStorageSetItem("yid", "");
    cc.audioEngine.stopAll();
    setTimeout( () =>{
        cc.game.restart();
    }, 100);
}

feedback() {
    c.log("客服");
    l.wx_head, l.user_name, l.wx_gender, l.user_id;
    var e = cc.sys.os === cc.sys.OS_IOS ? s.ios_help_center_url : s.android_help_center_url;
    d.showPage("HelpCenterPage", {
        url: e
    });
}

about() {
    c.log("打开关于界面");
    d.showPage("AboutPage");
}

clickClose() {
    this.hide();
}
}